package com.hhss.qishi.DPtest.designpattern.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 订阅的主题
 */
public class Subject implements Obvservable {

    /**
     * 气温
     */
    private float temp;

    /**
     * 风象
     */
    private float wind;

    /**
     * 待通知的观察者集合
     */
    private List<Observer> subscribers;

    public Subject(){
        subscribers = new ArrayList<>();
    }

    @Override
    public void addObserver(Observer observer) {
        subscribers.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        if (subscribers.indexOf(observer) > 0) {
            subscribers.remove(observer);
        }
    }

    @Override
    public void noticeObserver() {
        subscribers.stream().forEach(x -> x.update(temp, wind));
    }

    public void setTemp(float temp) {
        this.temp = temp;
    }

    public void setWind(float wind) {
        this.wind = wind;
    }
}
